/*
 * main.c
 *
 *  Created on: 4-Jun-2009
 *      Author: nrqm
 */

#include "arduino/WProgram.h"

#include "midi/midi.h"
#include "stepper/stepper.h"
#include "solenoid/solenoid.h"


/**
 * Function for printing debug statements to the serial port.
 */
void print(const char* str)
{
	uint8_t i;
	for (i=0; str[i] != 0; i++)
	{
		Serial.write(str[i]);
	}
}

/**
 * The Arduino millis function returns a 32-bit integer for some reason, and that compares unfavourably
 * with the 16-bit integers we're using, so we want to use a 16-bit timer.
 */
uint16_t millis16()
{
	return millis() & 0xFFFF;
}

// This is invoked when a pure virtual function is called.  See this:
//		http://ccgi.rowley.co.uk/support/faq.php?do=article&articleid=127
extern "C" void __cxa_pure_virtual()
{
	digitalWrite(13, HIGH);	// turn the LED on
	cli();
	while (1);	// infinite uninterruptible loop
}

void setup()
{
	sei();
}

void loop()
{
	for (;;)
	{
		Midi::GetInstance()->CheckMidi();
		Solenoid::GetInstance(MAIN)->CheckSolenoid();
		Solenoid::GetInstance(BASS)->CheckSolenoid();
		Solenoid::GetInstance(HI_HAT)->CheckSolenoid();
		Stepper::GetInstance()->CheckStepper();
	}
}

// This is the default main function used for Arduino sketches.
int main(void)
{
	init();

	setup();

	for (;;)
		loop();

	return 0;
}

